package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.pc.SpecificationAddPcReq;
import com.homedo.odin.solution.contract.pc.SpecificationDetailPcResp;
import com.homedo.odin.solution.contract.pc.SpecificationEditPcReq;
import com.homedo.odin.solution.contract.pc.SpecificationIdRequestBeanReq;
import com.homedo.odin.solution.contract.pc.SpecificationListResp;
import com.homedo.odin.solution.contract.pc.StandardIndexReq;
import com.homedo.odin.solution.contract.pc.USpecificationDetailResp;
import com.homedo.odin.solution.enumcode.basic.ErrorCodeEnum;
import com.homedo.odin.solution.enumcode.specification.SpecificationStatus;
import com.homedo.odin.solution.model.dto.SpecificationListResponseBean;
import com.homedo.odin.solution.model.entity.Specification;
import com.homedo.odin.solution.service.SpecificationServiceImpl;
import com.homedo.odin.solution.service.bizservice.mng.StandardsBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhaoxudong
 * @date: 2018/4/10 17:22
 * @Description:
 */
@Api(description = "行业规范", tags = {"Standard"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/standards")
public class StandardsController extends BaseController<StandardsBizService> {


    @Autowired
    SpecificationServiceImpl specificationService;

    @ApiOperation(value = "PC_获取行业规范信息列表", notes = "PC_获取行业规范信息列表", response = SpecificationListResp.class)
    @GetMapping
    public ResultData list(
            @ApiParam(value = "当前登录用户", required = false) @RequestParam(value = "userId", required = false) Integer userId,
            @ApiParam(value = "一级规范分类", required = false) @RequestParam(value = "firstClassify", required = false) Integer firstClassify,
            @ApiParam(value = "二级规范分类", required = false) @RequestParam(value = "secondClassify", required = false) Integer secondClassify,
            @ApiParam(value = "区域", required = false) @RequestParam(value = "zone", required = false) String zone,
            @ApiParam(value = "发布日期(0:升序 1:降序)", required = false) @RequestParam(value = "createTimeSort", required = false) Integer createTimeSort,
            @ApiParam(value = "我的(0:否 1:是)", required = false) @RequestParam(value = "isMy", required = false) Integer isMy,
            @ApiParam(value = "搜索关键字(0:否 1:是)", required = false) @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        SpecificationListResp resp = baseService
                .list(userId, firstClassify, secondClassify, zone, createTimeSort, isMy, keyword, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }


    @ApiOperation(value = "PC_新增行业规范信息", notes = "PC_新增行业规范信息")
    @PostMapping
    public ResultData add(@RequestBody SpecificationAddPcReq req) {
        baseService.add(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "PC_用户查看行业规范详细信息", notes = "PC_用户查看行业规范详细信息", response = USpecificationDetailResp.class)
    @GetMapping("/{userId}/{specificationId}")
    public ResultData basic(@ApiParam("用户id") @PathVariable("userId") Integer userId,
            @ApiParam("行业规范id") @PathVariable("specificationId") Long specificationId) {
        USpecificationDetailResp resp = baseService.basic(userId, specificationId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取行业规范详细信息", notes = "PC_获取行业规范详细信息", response = SpecificationDetailPcResp.class)
    @GetMapping("/{standardId}")
    public ResultData detail(@ApiParam(value = "行业规范id", required = true) @PathVariable("standardId") Long standardId,
            @ApiParam("用户id") @RequestParam(value = "userId", required = false) Integer userId,
            @ApiParam("会员等级") @RequestParam(value = "memberLevel", required = false) Integer memberLevel) {
        SpecificationDetailPcResp resp = baseService.detail(standardId, userId, memberLevel);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取行业规范详细信息", notes = "PC_获取行业规范详细信息")
    @PutMapping
    public ResultData edit(@RequestBody SpecificationEditPcReq req) {
        baseService.edit(req);
        return ResultData.newInstance();
    }

    /****************************liStsar*/

    @ApiOperation(value = "新增行业规范下载记录",notes = "新增行业规范下载记录",httpMethod = "POST")
    @PostMapping("/standards_download")
    public ResultData addUserSolutionDownload(@Valid SpecificationIdRequestBeanReq specificationIdRequest) {

        if(!NumberHelper.validatePositive(specificationIdRequest.getUserId())){
            throw  new BizException(Meta.CODE_451.getCode(), "用户id不合法!");
        }
        Long specificationId = specificationIdRequest.getSpecificationId();
        Specification specification = specificationService.getSpecification(specificationId);
        if(specification == null){
            throw new BizException(ErrorCodeEnum.NotExist.getDbValue(),"该行业规范不存在");
        }
        if(specification.getStatus().intValue() != SpecificationStatus.SHELVE.getDbValue()){
            throw new BizException(Meta.CODE_451.getCode(), "该行业规范不是上架中");
        }
        specificationService.addUserSolutionDownload(specificationIdRequest);
        return new ResultData(Meta.CODE_200,"新增行业规范下载记录成功!");
    }



    @GetMapping("/index/list")
    @ApiOperation(value = "获取首页查询规范信息列表", response = SpecificationListResponseBean.class, httpMethod = "GET")
    public ResultData queryIndexSolution(@ModelAttribute StandardIndexReq standardIndexReq){
        SpecificationListResp resp =  baseService.queryIndexSolution(standardIndexReq);
        return ResultData.newInstance(resp);
    }

    /****************************liend*/

}
